package cn.edu.scau.cmi.tancuier.client;

import java.util.ArrayList;
import java.util.List;

import cn.edu.scau.cmi.tancuier.adapter.Student;
import cn.edu.scau.cmi.tancuier.adapter.StudentSortAdapterOfClass;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class AdapterOfClassClient  extends Application{

	public static void main(String[] args){
		Application.launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		primaryStage.setTitle("Adapter of Class");
		List<Student> students = new ArrayList<Student>();
		
		FlowPane flowPane = new FlowPane();
		flowPane.setPadding(new Insets(10, 20, 10, 20));
		flowPane.setHgap(5);
		flowPane.setVgap(5);
		
		Label guide = new Label("Please input the student id and name: ");
		guide.setMinWidth(400);
		flowPane.getChildren().add(guide);
		
		Label id = new Label("ID:        ");
		flowPane.getChildren().add(id);
		TextField idText = new TextField();
		idText.setMinWidth(300);
		flowPane.getChildren().add(idText);
		Label name = new Label("Name: ");
		flowPane.getChildren().add(name);
		TextField nameText = new TextField();
		nameText.setMinWidth(300);
		flowPane.getChildren().add(nameText);
		
		Label space = new Label("                                                          ");
		flowPane.getChildren().add(space);
		Button sort = new Button("Add");
		flowPane.getChildren().add(sort);
		
		TextArea result = new TextArea();
		result.setEditable(false);
		result.setPrefSize(350, 400);
		flowPane.getChildren().add(result);
		
		sort.setOnAction(e->{
			Student student = new Student();
			student.setId(Integer.parseInt(idText.getText()));
			student.setName(nameText.getText());
			students.add(student);
			StudentSortAdapterOfClass adapter = new StudentSortAdapterOfClass();
			adapter.sortStudent(students);
			//display
			String clear = "";
			result.setText(clear);
			for(int i = 0; i < students.size(); i++) {
				
				result.appendText(Integer.toString(students.get(i).getId()) + " " + students.get(i).getName() + "\n");
			}
		});
		
		Scene scene = new Scene(flowPane, 400, 600);
		primaryStage.setScene(scene);
		primaryStage.show();
	}
}
